package com.liao.view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;

import com.google.gson.Gson;
import com.liao.view.adapter.VideoListViewAdapter;
import com.liao.view.domain.VideoInfo;
import com.liao.view.utils.LoadDataAsyncTask;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class VideoActivity extends AppCompatActivity implements LoadDataAsyncTask.GetDataCallBack {
    private ListView video_lv;
    private List<VideoInfo.ItemListBean> listBeans;
    private String url = "http://baobab.kaiyanapp.com/api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei6&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20";
    private VideoListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        initView();
    }

    private void initView() {
        video_lv = findViewById(R.id.video_lv);

        listBeans = new ArrayList<>();
        adapter = new VideoListViewAdapter(this, listBeans);
        video_lv.setAdapter(adapter);
        LoadDataAsyncTask asyncTask = new LoadDataAsyncTask(this, this);
        asyncTask.execute(url);
    }

    @Override
    public void getDataCallBack(String data) {
        if (!TextUtils.isEmpty(data)) {
            VideoInfo videoBean = new Gson().fromJson(data, VideoInfo.class);
            List<VideoInfo.ItemListBean> list = videoBean.getItemList();
            for (int i = 0; i < list.size(); i++) {
                VideoInfo.ItemListBean itemBean = list.get(i);
                String type = itemBean.getType();
                if (type.equals("video")) {
                    listBeans.add(itemBean);
                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        JzvdStd.releaseAllVideos();
    }
}
